package com.threeonemeeting.activity;

import java.lang.reflect.Field;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.list.EnrollFieldInfoList;
import com.threeonemeeting.model.EnrollFieldInfo;
import com.threeonemeeting.model.EnrollUserInfo;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.task.GetMeetingMoreDetailTask;
import com.threeonemeeting.task.HttpConst;
import com.threeonemeeting.util.MiscUtil;

public class EnrollUserDetailActivity extends BaseTabActivity implements
		ICallBack {

	@SuppressWarnings("unused")
	private int nJoinId;
	private int nMeetingId;
	private EnrollUserInfo uInfo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_enroll_user_detail);
		init("报名用户明细");

		Intent t = this.getIntent();
		if (t == null) {
			back();
			return;
		}

		nJoinId = t.getExtras().getInt("jid");
		nMeetingId = t.getExtras().getInt("eid");
		uInfo = (EnrollUserInfo) t.getExtras().getSerializable("info");
		if (uInfo == null) {
			back();
			return;
		}

		LinearLayout loadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
		ScrollView scrollViewLayout = (ScrollView) findViewById(R.id.scroll_view);
		loadingLayout.setVisibility(View.VISIBLE);
		scrollViewLayout.setVisibility(View.GONE);

		// 在界面上进行展示

		// 当前会议所用到的所有的字段,系统字段优先
		EnrollFieldInfoList fieldInfoList = DataCenter.getInstance().listEnrollField;
		if (fieldInfoList.size() == 0) {

			// 请求关于会议的额外补充信息
			GetMeetingMoreDetailTask task = new GetMeetingMoreDetailTask(this,
					this);
			task.nMeetingId = nMeetingId;
			Bundle params = new Bundle();
			params.putString("eid", String.valueOf(nMeetingId));
			params.putString(
					"uid",
					String.valueOf(DataCenter.getInstance().currentLoginUserInfo.nUserId));
			params.putString("pass",
					DataCenter.getInstance().currentLoginUserInfo.strPassword);
			TaskManager.addTask(task, params, true);
			return;
		} else {
			if (fieldInfoList.nMeetingId != nMeetingId) {
				fieldInfoList.clear();

				// 请求关于会议的额外补充信息
				GetMeetingMoreDetailTask task = new GetMeetingMoreDetailTask(
						this, this);
				task.nMeetingId = nMeetingId;
				Bundle params = new Bundle();
				params.putString("eid", String.valueOf(nMeetingId));
				params.putString(
						"uid",
						String.valueOf(DataCenter.getInstance().currentLoginUserInfo.nUserId));
				params.putString(
						"pass",
						DataCenter.getInstance().currentLoginUserInfo.strPassword);
				TaskManager.addTask(task, params, true);
				return;

			}
		}

		showEnrollField();
	}

	private LinearLayout addLineLayout(String strFieldName, String strFieldValue) {

		LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.lib_enroll_user_detail, null);

		TextView nameTextView = (TextView) layout.findViewById(R.id.field_name);
		nameTextView.setText(strFieldName);

		TextView valueTextView = (TextView) layout
				.findViewById(R.id.field_value);
		valueTextView.setText(strFieldValue);

		return layout;
	}

	public void doCallBack(Map<String, Object> map) {
		Integer msgType = (Integer) map.get(HttpConst.KEY_TASK_TYPE);
		if (msgType == null) {
			return;
		}

		Integer nError = (Integer) map.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null || nError != HttpUtil.ERROR_SUCCESS) {
			return;
		}

		// 获取到详情信息
		if (msgType == HttpConst.TASKTYPE_MOREMEETINGDETAIL) {

			showEnrollField();
		}

	}

	private void showSystemEnrollField(EnrollFieldInfo fInfo) {

		String strFieldName = fInfo.strName;

		LinearLayout topLayout = (LinearLayout) findViewById(R.id.line_layout);
		if (topLayout == null)
			return;

		Field f = null;
		try {
			f = uInfo.getClass().getField("str" + strFieldName);
		} catch (SecurityException e) {
		} catch (NoSuchFieldException e) {

			try {
				f = uInfo.getClass().getField("n" + strFieldName);
			} catch (SecurityException e1) {
			} catch (NoSuchFieldException e1) {
			}

		}

		if (f != null) {
			try {
				Object obj = f.get(uInfo);

				String strValue = "";
				if (obj instanceof Integer) {
					Integer iValue = (Integer) obj;
					if (iValue == 0)
						return;

					strValue = String.valueOf(iValue);

					if (fInfo.strName.equalsIgnoreCase(Const.Enroll.Department) == true) {
						strValue = DataCenter.getInstance().listDeptInfo
								.getDepartmentName(iValue);
					} else if (fInfo.strName.equalsIgnoreCase(Const.Enroll.Industries) == true) {
						strValue = DataCenter.getInstance().listTrades
								.getTradesName(iValue);
					} else if (fInfo.strName.equalsIgnoreCase(Const.Enroll.PosStatus) == true) {
						strValue = DataCenter.getInstance().listPosInfo
								.getPosInfoName(iValue);
					} else if (fInfo.strName.equalsIgnoreCase(Const.Enroll.BuyCount) == true) {
						strValue = String.valueOf(iValue) + "人";
					}

				} else if (obj instanceof String) {
					strValue = (String) obj;
					if (strValue.trim().equalsIgnoreCase("") == true
							|| strValue.trim().equalsIgnoreCase("null") == true)
						return;
				}

				LinearLayout layout = addLineLayout(fInfo.strDesc, strValue);
				LinearLayout.LayoutParams topLineLayoutLp = new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, MiscUtil.dp2px(45));
				topLayout.addView(layout, topLineLayoutLp);

				// 分隔线条
				LinearLayout splitterLineLayout = (LinearLayout) getLayoutInflater()
						.inflate(R.layout.lib_hline, null);
				LinearLayout.LayoutParams splitterLineLayoutLp = new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, MiscUtil.dp2px(1));
				topLayout.addView(splitterLineLayout, splitterLineLayoutLp);

			} catch (IllegalArgumentException e) {
			} catch (IllegalAccessException e) {
			}
		}

	}

	private void showCustomEnrollField(EnrollFieldInfo fInfo,
			int nCustomFieldIndex) {

		String strFieldName = fInfo.strName;

		LinearLayout topLayout = (LinearLayout) findViewById(R.id.line_layout);
		if (topLayout == null)
			return;

		Field f = null;
		try {
			f = uInfo.getClass().getField("strField" + nCustomFieldIndex);
		} catch (SecurityException e) {
		} catch (NoSuchFieldException e) {
		}

		if (f != null) {
			try {
				String strValue = (String) f.get(uInfo);

				LinearLayout layout = addLineLayout(strFieldName, strValue);
				LinearLayout.LayoutParams topLineLayoutLp = new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, MiscUtil.dp2px(45));
				topLayout.addView(layout, topLineLayoutLp);

				// 分隔线条
				LinearLayout splitterLineLayout = (LinearLayout) getLayoutInflater()
						.inflate(R.layout.lib_hline, null);
				LinearLayout.LayoutParams splitterLineLayoutLp = new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, MiscUtil.dp2px(1));
				topLayout.addView(splitterLineLayout, splitterLineLayoutLp);

			} catch (IllegalArgumentException e) {
			} catch (IllegalAccessException e) {
			}
		}

	}

	private void showEnrollField() {
		EnrollFieldInfoList fieldInfoList = DataCenter.getInstance().listEnrollField;

		int nCustomFieldIndex = 0;
		for (int i = 0; i < fieldInfoList.size(); i++) {

			EnrollFieldInfo fInfo = fieldInfoList.get(i);
			if (fInfo == null)
				continue;

			if (fInfo.eStatus == EnrollFieldInfo.EStatus.Hide)
				continue;

			if (fInfo.nFieldId != 0) {
				showCustomEnrollField(fInfo, nCustomFieldIndex);
				nCustomFieldIndex++;
			} else {
				showSystemEnrollField(fInfo);
			}

		}

		LinearLayout loadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
		ScrollView scrollViewLayout = (ScrollView) findViewById(R.id.scroll_view);
		loadingLayout.setVisibility(View.GONE);
		scrollViewLayout.setVisibility(View.VISIBLE);
	}

}
